package com.roc.web.action.terminal;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblInsInf;
import com.roc.pojo.YsbTermSnStatistics;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.Utils;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
/**
 * 云商宝终端统计
 * @author lenovo
 *
 */
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/ysbTermSnStatis")
public class YsbTermSnStatisticsAction extends PaginationBaseAction {

	private static final long serialVersionUID = 1L;

	static Logger logger = LogManager.getLogger(YsbTermSnStatisticsAction.class);

	private YsbTermSnStatisticsSearchCondition searchCondition;

	private List<TblInsInf> orgList = new ArrayList<TblInsInf>();

	private InputStream excelFile;
	
	private String excelName;

	@Autowired
	IPageService pagenitionService;
	
	@Autowired
	IPowerInfoService powerInfoService;
	/**
	 * 云商宝终端统计主页
	 * @Title: init
	 * @return
	 * @throws Exception
	 * @author 
	 * @date: 
	 *
	 */
	@Action(value = "init", results = { @Result(location = "/pages/terminal/YsbTermSnStatistics.jsp") })
	public String init() throws Exception {
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if( !op.getRoleType().equals(Role.SYSTEM.getCode()) 
				&& !op.getRoleType().equals(Role.O_MANAGER.getCode())
				&& !op.getRoleType().equals(Role.AGENT.getCode())){
			addActionError("此登陆账号没有权限");
			return ERROR;
		}
		org();
		return SUCCESS;
	}
    /**
     *  云商宝终端sn统计
     * @Title: queryTermSnStatis
     * @return
     * @throws Exception
	 * @author 付盼
	 * @date: 2018年3月7日 
     *
     */
	@Action(value = "queryTermSnStatis", results = { @Result(name = "success", location = "/pages/terminal/YsbTermSnStatistics.jsp"),
			@Result(name = "error", location = "/error.jsp")})
	// 查询终端
	public String queryTermSnStatis() throws Exception {

		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if (searchCondition == null) {
			searchCondition = (YsbTermSnStatisticsSearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new YsbTermSnStatisticsSearchCondition();
			}
		}
		try {
			org();
			if( !op.getRoleType().equals(Role.SYSTEM.getCode()) 
					&& !op.getRoleType().equals(Role.O_MANAGER.getCode())
					&& !op.getRoleType().equals(Role.AGENT.getCode())){
				addActionError("此登陆账号没有权限");
				return ERROR;
			}
	        searchCondition.setRoleType(op.getRoleType());// 角色
	        searchCondition.setAgentId(String.valueOf(op.getId()));// 当前账号id
			int rows = pagenitionService.countSQL(searchCondition);
	
			searchRsList = pagenitionService.findAllSQL(searchCondition,calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
	
			List<YsbTermSnStatistics> beanList = new ArrayList<YsbTermSnStatistics>();
			if (searchRsList != null && searchRsList.size() > 0) {
				Iterator iterator = searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					YsbTermSnStatistics bean = new YsbTermSnStatistics();
					String aff = "";
					if ( !"".equals( Constance.Null2EmptyTrim(obj[0]) ) ){
						aff = Utils.getSnAssefMap().get( Constance.Null2EmptyTrim(obj[0]) );
					}
					bean.setAssetFlag(aff);
					bean.setAllSum(Constance.Null2EmptyTrim(obj[1]));
					bean.setBrandSum(Constance.Null2EmptyTrim(obj[2]));;
					
					beanList.add(bean);
				}
			}
			searchRsList.clear();
			searchRsList.addAll(beanList);
			this.setTotalPage(this.calcuTotalPage(rows));	
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError(e.getMessage());
		}

		return SUCCESS;
	}

	private void org() {
		try {
			Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);

			Map<String,String> assefMap = Utils.getSnAssefMap();
			// 去掉 "公司代采"
			assefMap.remove("1");
			getRequest().setAttribute("assefMap", assefMap);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		ActionContext.getContext().put("merchantList", null);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE + "";
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (YsbTermSnStatisticsSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_YSB_SN_STATISTICS);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);

	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_YSB_SN_STATISTICS,
				searchCondition);
	}

	public List<TblInsInf> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<TblInsInf> orgList) {
		this.orgList = orgList;
	}

	public YsbTermSnStatisticsSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(YsbTermSnStatisticsSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public InputStream getExcelFile() {
		return excelFile;
	}

	public void setExcelFile(InputStream excelFile) {
		this.excelFile = excelFile;
	}

	public String getExcelName() {
		return excelName;
	}

	public void setExcelName(String excelName) {
		this.excelName = excelName;
	}

}
